<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let arr = [1, 2, 3, 4, 5]
    let arr2 = arr.map(function (item, index) {
      return item + 1
    }, [5, 4, 3, 2, 1])
    console.log(arr2);
    // Array.prototype.map2 = function (callback, thisArg) {
    //   if (this === null) {
    //     throw new TypeError('this is null')
    //   }
    //   if (typeof callback !== 'function') {
    //     throw new TypeError('callback is not a function')
    //   }
    //   let res = []
    //   for (let i = 0; i < this.length; i++) {
    //     res[i] = callback.call(thisArg, this[i], i, this)
    //   }
    //   return res
    // }
    Array.prototype.map2 = function (callback, thisArg) {
      if (this === null) {
        throw new Error('this is null')
      }
      if (typeof callback !== 'function') {
        throw new Error('this must be a function')
      }
      let result = []
      for (let i = 0; i < this.length; i++) {
        result.push(callback.call(thisArg, this[i], i, this))
      }
      return result
    }
    let arr3 = arr.map2(function (item) {
      return item + 1
    })
    console.log(arr3);
  </script>
</body>

</html>